package com.zang.backtrack;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZVerify
 * @since 2022/11/12 16:22
 * @see <a href="https://leetcode.cn/problems/letter-combinations-of-a-phone-number/description/">...</a>
 **/
public class 电话号码的组合 {

    List<String> result = new ArrayList<>();
    StringBuilder temp = new StringBuilder();
    public List<String> letterCombinations(String digits) {

        if(digits==null || digits.length()==0) return result;

        // 值map
        String[] map = new String[] {"","","abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

        backtrack(digits, map , 0);
        return result;
    }

    private void backtrack(String digits, String[] map, int startIndex) {

        // 遍历完字符串结束
        if (startIndex == digits.length()) {
            result.add(temp.toString());
            return;
        }

        // 按顺序拿到字符，然后得到所对应的字符串
        char c1 = digits.charAt(startIndex);
        String s = map[c1-'0'];
        // 遍历当前的字符串然后对下一个字符所对应的每一个字符进行匹配
        for (int i = 0; i < s.length(); i++) {
            // 拿到当前案件的第i个字符
            char c = s.charAt(i);
            temp.append(c);
            // 进入下一次去拿第二个按键的字符
            backtrack(digits,map,startIndex+1);
            // 回溯
            temp.deleteCharAt(temp.length() - 1);
        }
    }


}
